﻿using System;
using YAXLib;
using System.Globalization;
using TomanuExtensions;

namespace RaytracerLib.Resamplers.Filters
{
    public class LanczosFilter : Filter
    {
        private const double DEFAULT_TAU = 3;
        private double m_tau = DEFAULT_TAU;

        [YAXNode]
        public double Tau
        {
            get
            {
                return m_tau;
            }
            set
            {
                m_tau = value;
                m_ray = value;
            }
        }

        public LanczosFilter()
            : base(DEFAULT_TAU)
        { 
        }

        public override string Name 
        {
            get
            {
                return "Lanczos";
            }
        }

        public override FilterType FilterType
        {
            get
            {
                return FilterType.Lanczos;
            }
        }

        public override double Evaluate(double a_value)
        {
            double v = Math.Abs(a_value);

            if (v > Tau)
                return 0;

            if (v < Constants.DOUBLE_PRECISION)
                return 1;

            v = v * MathExtensions.PI;
            double p3 = v / Tau;
            return Math.Sin(v) * Math.Sin(p3) / (v * p3);
        }

        public override string ToString()
        {
            return String.Format(CultureInfo.InvariantCulture, "Lanczos, Tau: {0}", Tau);
        }
    }
}